<?php

/*
 * Copyright (C) 2014 University of Pittsburgh
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once('Constants.inc');
require_once('database/ReferenceTableDAO.inc');

class StaticFunctions {
  
  public static function indent($how_many = 0, $indent_size = 0) {
    $retVal = '';
    for($i = 0; $i < $how_many; $i++) {
      for($j = 0; $j < $indent_size; $j++) {
        $retVal .= ' ';
      }
    }
    
    return $retVal;
  }
  
  public static function detect_os($user_agent = '') {
    
    if(preg_match('/Linux/', $user_agent)) {
      return Constants::LINUX_OS;
    } elseif(preg_match('/Win/', $user_agent)) {
       return Constants::WINDOWS_OS;
    } elseif(preg_match('/Mac/', $user_agent)) {
      return Constants::MAC_OS;
    } else {
      return Constants::OTHER_OS;
    }
  }
  
  public static function load_reference_data() {
    if(function_exists('apc_store')) {
      foreach(Constants::$reference_table_names as $ref_table_code => $ref_table_name) {
        if(!apc_exists($ref_table_name)) {
          $ref_table_dao = new ReferenceTableDAO();
          $ref_table_dao->set_table($ref_table_code);
        
          $table_data = $ref_table_dao->get_table_data_as_array();
          apc_store($ref_table_name, new ArrayObject($table_data));
        }
      }
    } elseif(function_exists('wincache_ucache_set')) {
      foreach(Constants::$reference_table_names as $ref_table_code => $ref_table_name) {
        if(!wincache_ucache_exists($ref_table_name)) {
          $ref_table_dao = new ReferenceTableDAO();
          $ref_table_dao->set_table($ref_table_code);
      
          $table_data = $ref_table_dao->get_table_data_as_array();
          wincache_ucache_set($ref_table_name, new ArrayObject($table_data));
        }
      }
    }
  }
  
  public static function delete_reference_data() {
    if(function_exists('apc_delete')) {
      foreach(Constants::$reference_table_names as $ref_table_code => $ref_table_name) {
        if(apc_exists($ref_table_name)) {
          apc_delete($ref_table_name);
        }
      }
    } elseif(function_exists('wincache_ucache_delete')) {
      foreach(Constants::$reference_table_names as $ref_table_code => $ref_table_name) {
        if(wincache_ucache_exists($ref_table_name)) {
          wincache_ucache_delete($ref_table_name);
        }
      }
    }
  }
  
  public static function get_reference_data($ref_table_code = 0) {
    if(function_exists('apc_fetch')) {
      $tmp = apc_fetch(Constants::$reference_table_names[$ref_table_code]);
      $table_data = $tmp->getArrayCopy();
      
      if(isset($table_data)) {
        return $table_data;
      }
    } elseif(function_exists('wincache_ucache_get')) {
      $tmp = wincache_ucache_get(Constants::$reference_table_names[$ref_table_code]);
      $table_data = $tmp->getArrayCopy();
      
      if(isset($table_data)) {
        return $table_data;
      }
    } else {
      $ref_table_dao = new ReferenceTableDAO();
      $ref_table_dao->set_table($ref_table_code);
      $table_data = $ref_table_dao->get_table_data_as_array();
      if(isset($table_data)) {
        return $table_data;
      }
    }
    return NULL;
  }
  
  public static function get_sitebase() {
    return 'http://' . $_SERVER["HTTP_HOST"] . '/Phasys_LENA';
  }

}

?>